<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" class="my-4" @click="downloadTemplate"> 下载模版 </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '下载',
              onClick: handleDownload.bind(null, record),
            },{
              label: '重试',
              onClick: handleRetry.bind(null, record),
            }
          ]"
        />
      </template>
    </BasicTable>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';

import { useGlobSetting } from '/@/hooks/setting';
const globSetting = useGlobSetting();

import { BasicTable, useTable, TableAction } from '/@/components/Table';
import {orderPage, orderRetry, orderDelete, orderUpload} from '/@/api/citic/order';
import { getBasicColumns } from "/@/views/citic/order/orderTaskData";
import { useDrawer } from '/@/components/Drawer';

export default defineComponent({
  name: 'CiticOrder',
  components: { BasicTable, TableAction },
  setup() {
    const [registerDrawer, { openDrawer }] = useDrawer();
    const [registerTable, { reload }] = useTable({
      title: '订单补推记录',
      api: orderPage,
      fetchSetting: {
        pageField: 'pageNum',
        totalField: 'totalCount',
        listField: 'list',
        sizeField: 'pageSize',
      },
      columns: getBasicColumns(),
      formConfig: {
        labelWidth: 120,
        schemas: [
          {
            field: 'orderUpload',
            label: '提交补推订单号',
            component: 'Upload',
            colProps: {span: 8},
            rules: [{required: true, message: '请选择上传文件'}],
            componentProps: ({schema, tableAction, formActionType, formModel}) => {
              return {
                api: orderUpload,
                maxSize: 20,
                accept: ['xlsx'],
                showPreviewNumber: false,
                emptyHidePreview: true,
                onChange: e => {
                  const {reload} = tableAction
                  reload()
                }
              };
            },
          },
        ],
        showSubmitButton: false,
        showResetButton: false,
      },
      useSearchForm: true,
      showTableSetting: true,
      bordered: true,
      showIndexColumn: false,
      actionColumn: {
        width: 100,
        title: '操作',
        dataIndex: 'action',
        slots: { customRender: 'action' },
        fixed: undefined,
      },
    });

    function handleCreate() {
      openDrawer(true, {
        isUpdate: false,
      });
    }

    function handleEdit(record: Recordable) {
      openDrawer(true, {
        record,
        isUpdate: true,
      });
    }

    function handleDelete(record: Recordable) {
      orderDelete({
        taskId: record.taskId
      });
      reload();
    }

    function handleSuccess() {
      reload();
    }

    function handleRetry(record: Recordable) {
      orderRetry({
        taskId: record.taskId
      });
      reload();
    }

    async function handleDownload (record: Recordable) {
      window.location.href = globSetting.apiUrl + "/citic/order/export/" + record.taskId;
    }

    function downloadTemplate() {
      window.location.href = "../static/template.xlsx";
    }

    return {
      registerTable,
      registerDrawer,
      handleCreate,
      handleEdit,
      handleDelete,
      handleSuccess,
      handleDownload,
      handleRetry,
      downloadTemplate,
    };
  },
});
</script>
